package com.gs.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gs.pojo.Attribute;
import com.gs.pojo.Passenger;
import com.gs.pojo.ReceiveTarget;
import com.gs.service.passenger.PassengerService;
import com.gs.service.receiveTarget.ReceiveTargetService;
import com.gs.utils.Page;

@Controller
@RequestMapping("/ReceiveTarget")
public class ReceiveTargetController {
	@Resource
	private ReceiveTargetService receiveTargetService;
	@Resource
	private PassengerService passengerService;
	
	@RequestMapping("/tolist.do")
	public String toList(Model model,@RequestParam(value="currentPage",required=false,defaultValue="1")Integer currentPage,
			@RequestParam(value="txtname",required=false,defaultValue="")String txtname){
		Page<ReceiveTarget> page=new Page<ReceiveTarget>();
		page.setCurrentPage(currentPage);
		page.setTotal(receiveTargetService.receiveTargetCount(txtname));
		List<ReceiveTarget> receiveTargets=receiveTargetService.getReceiveTargetList(txtname,page.getCurrentPage(),page.getPageSize());
		for (ReceiveTarget receiveTarget : receiveTargets) {
			receiveTarget.setTargetTypeName(passengerService.getInformationById(receiveTarget.getTargetTypeID()));
		}
		page.setResult(receiveTargets);
		model.addAttribute("list", page);
		return "receivetarget/list";
	}
	@RequestMapping(value="/toadd.do",method=RequestMethod.GET)
	public String toAdd(Model model){
		List<Attribute> attribute= passengerService.getAttributeByAttributeID(14);
		model.addAttribute("listOne", attribute);
		return "receivetarget/add";
	}
	//验证团队编号信息
	@RequestMapping(value="/YZ.do")
	@ResponseBody
	public Object YZ(String teamCode,HttpServletRequest request){
		Integer num=receiveTargetService.YZ(teamCode);
		return num;
	}
	@RequestMapping(value="/add.do",method=RequestMethod.POST)
	public String doAdd(ReceiveTarget receiveTarget){
		receiveTargetService.addReceiveTarget(receiveTarget);
		return "redirect:/ReceiveTarget/tolist.do";
	}
	@RequestMapping("toupdate.do")
	public String toupdate(Integer id,Model model){
		ReceiveTarget receiveTarget=receiveTargetService.getReceiveTargetByID(id);
		model.addAttribute("list", receiveTarget);
		List<Attribute> attribute= passengerService.getAttributeByAttributeID(14);
		model.addAttribute("listOne", attribute);
		return "receivetarget/update";
	}
	@RequestMapping("update.do")
	public String doupdate(ReceiveTarget receiveTarget){
		receiveTargetService.updateReceiveTarget(receiveTarget);
		return "redirect:/ReceiveTarget/tolist.do";
	}
	@RequestMapping("delete.do")
	public String del(Integer id){
		receiveTargetService.delReceiveTargetByID(id);
		return "redirect:/ReceiveTarget/tolist.do";
	}
}
